/*
@ClassName DataScheduleServiceImpl
@Description TODO
@Author SubUp
@Date   2022/8/4
@Version 1.0
*/

package com.sunjob.mrojobschedule.service.impl;

import com.sunjob.common.constant.MROConstant;
import com.sunjob.common.domain.scheduleServer.*;
import com.sunjob.common.domain.scheduleServer.orm.JobResult;
import com.sunjob.common.domain.scheduleServer.orm.PlanResult;
import com.sunjob.common.mroAnno.MethodVerify;
import com.sunjob.mrojobschedule.mapper.ProductScheduleMapper;
import com.sunjob.mrojobschedule.service.JobScheduleService;
import com.sunjob.mrojobschedule.service.PlanScheduleService;
import com.sunjob.mrojobschedule.service.ProductScheduleService;
import com.sunjob.mrojobschedule.util.ScheduleUtils;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

@Service
public class ProductScheduleServiceImpl<T> implements ProductScheduleService<T> {
    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @Resource
    private ProductScheduleMapper productScheduleMapper;

    @Resource
    private PlanScheduleService planScheduleService;

    @Resource
    private JobScheduleService jobScheduleService;

    /**
     * 进入页面显示的物品信息
     * @param kindId
     * @return
     */
    @Override
    @Transactional
    public List<T> selectProducts(Integer kindId) {
        return null;
    }

    /**
     * 搜索kindId、productId得到对应物品
     * @param kindId 分类Id
     * @param productId 物品Id
     * @return
     */
    @Override
    @Transactional
    public Map selectProduct(Integer kindId, Integer productId) {
        return null;
    }

    /**
     * 点击生成计划弹出窗口的配置，有计划生存周期、检修依据条件、计划性质
     * @return
     */
    @Override
    public Map generatePlanList() {
        Map lifeCycleMap = selectLifeCycleParam();
        Map conditionMap = selectExamineConditionParam();
        Map natureMap = selectPlanNatureParam();
        Map patternMap = selectExaminePatternParam();
        Map<String,Object> map = new ConcurrentHashMap<>();
        map.put("life-cycle",lifeCycleMap);
        map.put("examine-condition",conditionMap);
        map.put("examine-pattern",patternMap);
        map.put("plan-nature",natureMap);
        return map;
    }

    @Override
    public Map generateJobList() {
        Map levelMap = selectJobLevelParam();
        /*技术员 工号--姓名---职位*/
        Map<String,Object> map = new ConcurrentHashMap<>();
        map.put("job-level",levelMap);
        map.put("staff","从人力资源服务调用");
        return map;
    }

    /**
     * 获得审批方式参数
     * @return
     */
    @Override
    @Transactional
    public Map selectExaminePatternParam() {
        Map<String, String> examinePatternParam = examinePatternParam();
        if(examinePatternParam.size() != 0)
            return examinePatternParam;
        List<ExaminePattern> examinePatterns = productScheduleMapper.examinePatternList();
        Map<String,String> patternsMap = new ConcurrentHashMap<>();
        for (ExaminePattern ep:examinePatterns
             ) {
            patternsMap.put(ScheduleUtils.transferStringByInteger(ep.getPatternCode()),ep.getPatternExplain());
        }
        return patternsMap;
    }

    @Override
    @Transactional
    public Map selectExamineChoiceParam() {
        Map<String, String> examineChoicesParam = examineChoicesParam();
        if(examineChoicesParam.size() != 0)
            return examineChoicesParam;
        List<ExamineChoice> examineChoices = productScheduleMapper.examineChoiceList();
        Map<String,String> choicesMap = new ConcurrentHashMap<>();
        for (ExamineChoice ec:examineChoices
        ) {
            choicesMap.put(ScheduleUtils.transferStringByInteger(ec.getChoiceCode()),ec.getChoiceExplain());
        }
        return choicesMap;
    }

    @Override
    @Transactional
    public Map selectExamineStateParam() {
        Map<String, String> examineStatesParam = examineStatesParam();
        if(examineStatesParam.size() != 0)
            return examineStatesParam;
        List<ExamineState> examineStates = productScheduleMapper.examineStateList();
        Map<String,String> stateMap = new ConcurrentHashMap<>();
        for (ExamineState es:examineStates
        ) {
            stateMap.put(ScheduleUtils.transferStringByInteger(es.getStateCode()),es.getStateExplain());
        }
        return stateMap;
    }

    @Override
    @Transactional
    public Map selectExamineConditionParam() {
        Map<String, String> conditionStatesParam = examineConditionStatesParam();
        if(conditionStatesParam.size() != 0)
            return conditionStatesParam;
        List<ExamineCondition> examineConditions = productScheduleMapper.examineConditionList();
        Map<String,String> conditionMap = new ConcurrentHashMap<>();
        for (ExamineCondition ec:examineConditions
             ) {
            conditionMap.put(ScheduleUtils.transferStringByInteger(ec.getConditionCode()),ec.getConditionExplain());
        }
        return conditionMap;
    }

    @Override
    @Transactional
    public Map selectJobFeedBackParam() {
        Map<String, String> jobFeedbackParam = jobFeedbackParam();
        if(jobFeedbackParam.size() != 0)
            return jobFeedbackParam;
        List<JobFeedBack> jobFeedBacks = productScheduleMapper.jobFeedBackList();
        Map<String,String> feedBackMap = new ConcurrentHashMap<>();
        for (JobFeedBack jfb:jobFeedBacks
             ) {
            feedBackMap.put(ScheduleUtils.transferStringByInteger(jfb.getFeedbackCode()),jfb.getFeedbackExplain());
        }
        return feedBackMap;
    }

    @Override
    @Transactional
    public Map selectJobLevelParam() {
        Map<String, String> jobLevelParam = jobLevelParam();
        if(jobLevelParam.size() !=0)
            return jobLevelParam;
        List<JobLevel> jobLevels = productScheduleMapper.jobLevelList();
        Map<String,String> levelMap = new ConcurrentHashMap<>();
        for (JobLevel jl:jobLevels
             ) {
            levelMap.put(ScheduleUtils.transferStringByInteger(jl.getLevelCode()),jl.getLevelExplain());
        }
        return levelMap;
    }

    @Override
    @Transactional
    public Map selectPlanNatureParam() {
        Map<String, String> planNatureParam = planNatureParam();
        if (planNatureParam.size() != 0)
            return planNatureParam;
        List<PlanNature> planNatures = productScheduleMapper.planNatureList();
        Map<String,String> planNatureMap = new ConcurrentHashMap<>();
        for (PlanNature pn:planNatures
             ) {
            planNatureMap.put(ScheduleUtils.transferStringByInteger(pn.getPlanNatureCode()),pn.getPlanNatureExplain());
        }
        return planNatureMap;
    }

    @Override
    @Transactional
    public Map selectPlanStateParam() {
        Map<String, String> planStateParam = planStateParam();
        if(planStateParam.size() !=0)
            return planStateParam;
        List<PlanState> planStates = productScheduleMapper.planStateList();
        Map<String,String> planStateMap = new ConcurrentHashMap<>();
        for (PlanState ps:planStates
             ) {
            planStateMap.put(ScheduleUtils.transferStringByInteger(ps.getPlanStateCode()),ps.getPlanStateExplain());
        }
        return planStateMap;
    }

    @Override
    @Transactional
    public Map selectScheduleShowParam() {
        Map<String, String> showStatesParam = showStatesParam();
        if(showStatesParam.size() != 0)
            return showStatesParam;
        List<ScheduleShow> scheduleShows = productScheduleMapper.scheduleShowList();
        Map<String,String> showMap = new ConcurrentHashMap<>();
        for (ScheduleShow ss:scheduleShows
             ) {
            showMap.put(ScheduleUtils.transferStringByInteger(ss.getShowCode()),ss.getShowExplain());
        }
        return showMap;
    }

    @Override
    public Map selectLifeCycleParam() {
        Map<String, String> lifeCycleStatesParam = lifeCycleStatesParam();
        if(lifeCycleStatesParam.size() != 0)
            return lifeCycleStatesParam;
        List<LifeCycle> lifeCycles = productScheduleMapper.lifeCycleList();
        Map<String,String> lifeCyCleMap = new ConcurrentHashMap<>();
        for (LifeCycle lc:lifeCycles
             ) {
            lifeCyCleMap.put(ScheduleUtils.transferStringByInteger(lc.getLifeCycleCode()),lc.getLifeCycleExplain());
        }
        return lifeCyCleMap;
    }

    /**
     * 数据
     * 参数  时间展示、作业级别
     * @param scheduleId
     * @return
     */
    @Override
    public Map scheduleJobMenu(String scheduleId) {
        Map<String,Object> map = new ConcurrentHashMap<>();
        Map levelMap = selectJobLevelParam();
        Map showMap = selectScheduleShowParam();
        List<JobResult> jobResults = jobScheduleService.selectJobOrmList("1");
        map.put("job-level",levelMap);
        map.put("schedule-show",showMap);
        map.put("job-data",jobResults);
        return map;
    }

    /**
     * 数据
     * 参数 时间展示、计划生存周期
     * @param scheduleId
     * @return
     */
    @Override
    public Map schedulePlanMenu(String scheduleId) {
        Map planNatureParam = selectPlanNatureParam();
        Map showMap = selectScheduleShowParam();
        List<PlanResult> planResults = planScheduleService.selectPlanResultByNatureId("0");
        Map<String,Object> map = new ConcurrentHashMap<>();
        map.put("plan-nature",planNatureParam);
        map.put("schedule-show",showMap);
        map.put("plan-data",planResults);
        return map;
    }

    @Override
    public Integer faultHandlerByExamine(PlanSchedule plan) {
        plan.setExaminePattern(1);


        return null;
    }

    @Override
    @MethodVerify
    public Integer faultHandler(JobSchedule job) {
        //自动审批、故障处理、自定义检、执行依据
        PlanSchedule plan = job.getPlanSchedule();
        plan.setExamineChoice(1);
        plan.setExaminePattern(1);
        plan.setPlanNature(3);
        plan.setLifeCycle(8);
        plan.setExamineCondition(4);
        System.out.println(plan);
        Integer var1 = planScheduleService.addSave(plan);
        //紧急任务
        job.setJobLevel(1);
        Integer var2 = jobScheduleService.insertJob(job);
        System.out.println(var1&var2);
        return var1&var2;
    }

    @Override
    public Integer censusTransformation(String planNatureId) {
        return null;
    }


    public Map<Integer,String> joint(List entries,Class clazz){
        Map<Integer,String> concurrentHashMap = new ConcurrentHashMap();
        String clazzSimpleName = clazz.getSimpleName();
        if("LifeCycle".equals(clazzSimpleName)){

        }


        return concurrentHashMap;
    }

    public Map handlerResume(List list1,List list2,List list3){
        return null;
    }

    public Map<String,String> jobLevelParam(){
        HashOperations<String,String,String> hashOperations = redisTemplate.opsForHash();
        Map<String,String> entries = hashOperations.entries(MROConstant.SCHEDULE_JOB_LEVEL);
        return entries;
    }

    public Map<String,String> jobFeedbackParam(){
        HashOperations<String,String,String> hashOperations = redisTemplate.opsForHash();
        Map<String,String> entries = hashOperations.entries(MROConstant.SCHEDULE_JOB_FEEDBACK);
        return entries;
    }

    public Map<String,String> planStateParam(){
        HashOperations<String,String,String> hashOperations = redisTemplate.opsForHash();
        Map<String,String> entries = hashOperations.entries(MROConstant.SCHEDULE_PLAN_STATE);
        return entries;
    }
    public Map<String,String> planNatureParam(){
        HashOperations<String,String,String> hashOperations = redisTemplate.opsForHash();
        Map<String,String> entries = hashOperations.entries(MROConstant.SCHEDULE_PLAN_NATURE);
        return entries;
    }
    public Map<String,String> lifeCycleStatesParam(){
        HashOperations<String,String, String> hashOperations = redisTemplate.opsForHash();
        Map<String,String> entries = hashOperations.entries(MROConstant.SCHEDULE_LIFE_CYCLE_STATE);
        return entries;
    }

    public Map<String, String> examineStatesParam(){
        HashOperations<String,String, String> hashOperations = redisTemplate.opsForHash();
        Map<String,String> entries = hashOperations.entries(MROConstant.SCHEDULE_EXAMINE_STATE);
        return entries;
    }

    public Map<String, String> examineConditionStatesParam(){
        HashOperations<String,String, String> hashOperations = redisTemplate.opsForHash();
        Map<String,String> entries = hashOperations.entries(MROConstant.SCHEDULE_EXAMINE_CONDITION_STATE);
        return entries;
    }

    public Map<String,String> showStatesParam(){
        HashOperations<String,String, String> hashOperations = redisTemplate.opsForHash();
        Map<String,String> entries = hashOperations.entries(MROConstant.SCHEDULE_SHOW_STATE);
        return entries;
    }

    public Map<String,String> examineChoicesParam(){
        HashOperations<String,String, String> hashOperations = redisTemplate.opsForHash();
        Map<String,String> entries = hashOperations.entries(MROConstant.SCHEDULE_EXAMINE_CHOICE);
        return entries;
    }

    public Map<String,String> examinePatternParam(){
        HashOperations<String,String, String> hashOperations = redisTemplate.opsForHash();
        Map<String,String> entries = hashOperations.entries(MROConstant.SCHEDULE_EXAMINE_PATTERN);
        return entries;
    }

}
